package name.jiaosha.air.system.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Part;
import name.jiaosha.air.company.domain.PartPackage;
import name.jiaosha.air.company.service.IGoodsService;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

/**
 * 配件字典
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin")
public class PartController {

	@Autowired
	private IGoodsService goodsService;

	@RequestMapping("/partList")
	public ModelAndView getPartList() {
		PageInfo<Part> pageInfo = null;
		if (!AppConstants.DEBUG) {
			List<Part> list = new ArrayList<Part>();
			for (int i = 1; i <= 20; i++) {
				Part part = new Part();
				part.setName("银焊条" + i);
				part.setBrand("青岛宏泰" + i);
				part.setUnit("个");
				part.setUnitPrice(new BigDecimal(50));
				list.add(part);
			}

		} else {

			pageInfo = goodsService.getPartListByComId(AppConstants.comId, 1,
					AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createPart")
	public ModelAndView createPart(@RequestBody String jsonData) {

		if (!AppConstants.DEBUG) {

		} else {
			ObjectMapper mapper = new ObjectMapper();
			try {
				Map<String, Object> data = mapper
						.readValue(jsonData, Map.class);

				byte[] b = mapper.writeValueAsBytes(data.get("part"));
				Part part = mapper.readValue(b, 0, b.length, Part.class);
				b = mapper.writeValueAsBytes(data.get("packageItems"));
				List<PartPackage> packageItems = mapper.readValue(b, 0,
						b.length, new TypeReference<ArrayList<PartPackage>>() {
						});
				part.setPackageItems(packageItems);

				part.setName(part.getName().trim());
				part.setComId(AppConstants.comId);
				part.setBrand(part.getBrand().trim());
				goodsService.createPart(part);

				for (PartPackage item : packageItems) {

					System.out.println(item);
				}
			} catch (JsonParseException e) {

				e.printStackTrace();
			} catch (JsonMappingException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updatePart")
	public ModelAndView updatePart(@RequestBody Part part) {

		if (!AppConstants.DEBUG) {

		} else {

			part.setName(part.getName().trim());
			part.setComId(AppConstants.comId);
			part.setBrand(part.getBrand().trim());
			goodsService.updatePart(part);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/partInfo")
	public ModelAndView getPartInfo(@RequestParam int id) {

		ModelMap model = new ModelMap();
		if (!AppConstants.DEBUG) {

		} else {

			Part part = goodsService.getPartById(id);
			if (part != null) {

				List<PartPackage> list = goodsService
						.getPartPackageByPartId(part.getId());
				part.setPackageItems(list);
			}
			model.put("data", part);
		}
		model.put("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
